package ebj.common.aws;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.util.StringUtils;

public class AwsFileUpload {
    public static void main(String[] args) {
        AmazonS3 conn = AwsConn.getConn();
        // s3://druid.yeahmobi.com/query.result/report_id-m201405301630-5921815181162778289.csv.gz
//        String bucketName = "druid.yeahmobi.com";
//        String fileName = "query.result/report_id-m201405301630-5921815181162778289.csv.gz";
        String bucketName = "download.ymtech.com";
        String fileName = "apk_files/wong888.bbpuzzle.apk";
        // AwsConn.setAccessable(conn, bucketName, fileName);
        String fileUrl = AwsConn.generateHttpDownloadUrl(conn, bucketName, fileName);
        System.out.println(fileUrl);
        AwsConn.showAllBuckets(conn);
        AwsConn.showBucketContents(conn, bucketName);
    }
}


class AwsConn{
//    private static String accessKey = "AKIAIIRBULVIK2HAJF6A";
//    private static String secretKey = "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";
    private static String accessKey = "AKIAI773WFRGRKX5RXAA";
    private static String secretKey = "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";
    private static AmazonS3 conn = null;
    
    static{
        conn = getAwsConn();
    }
    
    private static AmazonS3 getAwsConn() {
        AmazonS3 conn = null;
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTP);

        conn = new AmazonS3Client(credentials, clientConfig);
        // conn.setEndpoint("endpoint.com");
        return conn;
    }
    
    public static AmazonS3 getConn() {
        return conn;
    }
    
    public static Bucket createBucket(AmazonS3 conn, String bucketName) {
        Bucket bucket = null;
        bucket = conn.createBucket(bucketName);
        return bucket;
    }
    
    public static void showAllBuckets(AmazonS3 conn) {
        List<Bucket> buckets = conn.listBuckets();
        for (Bucket bucket : buckets) {
                System.out.println(bucket.getName() + "\t" 
                        + StringUtils.fromDate(bucket.getCreationDate()));
        }
    }
    
    
    public static void showBucketContents(AmazonS3 conn, String bucketName) {
        ObjectListing objects = conn.listObjects(bucketName);
        do {
                for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                        System.out.println(objectSummary.getKey() + "\t" +
                                objectSummary.getSize() + "\t" +
                                StringUtils.fromDate(objectSummary.getLastModified()));
                }
                objects = conn.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());
    }
    
    
    public static void deleteBucket(AmazonS3 conn, String bucketName) {
        conn.deleteBucket(bucketName);
    }
    
    public static PutObjectResult writeFile(AmazonS3 conn, String bucketName, String fileName, String contents) {
        ByteArrayInputStream input = new ByteArrayInputStream(contents.getBytes());
        return conn.putObject(bucketName, fileName, input, new ObjectMetadata());
    }
    
    public static void setAccessable(AmazonS3 conn, String bucketName, String fileName) {
        conn.setObjectAcl(bucketName, fileName, CannedAccessControlList.PublicRead);
    }
    
    public static void delObject(AmazonS3 conn, String bucketName, String fileName) {
        conn.deleteObject(bucketName, fileName);
    }
    
    public static void saveObjectToLocal(AmazonS3 conn, String bucketName, String fileName, String dstPath) {
        conn.getObject(
                new GetObjectRequest(bucketName, fileName),
                new File(dstPath + "/" + fileName)
        );
    }
    
    public static String generateHttpDownloadUrl(AmazonS3 conn, Bucket bucket, String fileName) {
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket.getName(), fileName);
        URL httpUrl = conn.generatePresignedUrl(request);
        return httpUrl.toString();
    }
    
    public static String generateHttpDownloadUrl(AmazonS3 conn, String bucketName, String fileName) {
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, fileName);
        URL httpUrl = conn.generatePresignedUrl(request);
        return httpUrl.toString();
    }
}
